package io.renren.modules.dangan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.dangan.dao.DaUserFlhDao;
import io.renren.modules.dangan.entity.DaUserFlhEntity;
import io.renren.modules.dangan.service.DaUserFlhService;


@Service("daUserFlhService")
public class DaUserFlhServiceImpl extends ServiceImpl<DaUserFlhDao, DaUserFlhEntity> implements DaUserFlhService {
    @CreateCache(name="flhCache",cacheType= CacheType.LOCAL,localExpire = 3600,localLimit = 3000)
    private Cache<Integer,List<String>> flhCache;

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer userId=Integer.parseInt((String) params.getOrDefault("userId","0"));
        IPage<DaUserFlhEntity> page = this.page(
                new Query<DaUserFlhEntity>().getPage(params),
                new QueryWrapper<DaUserFlhEntity>()
                .eq(userId>0,"user_id",userId)
        );
        Set<String> flhs=page.getRecords().stream().map(DaUserFlhEntity::getFlh).collect(Collectors.toSet());
        List<Long> roleIds= sysUserRoleService.queryRoleIdList(userId.longValue());
        roleIds.forEach(roleId->{
            SysRoleEntity roleEntity = sysRoleService.findRoleDetailById(roleId);
            if(ObjectUtil.isNotEmpty(roleEntity)){
                flhs.addAll(roleEntity.getFlhs());
            }
        });
        flhCache.put(userId,new ArrayList<>(flhs));
        return new PageUtils(page);
    }

    @Override
    public boolean saveBatch(int userId, List<String> flhs) {
        try {
            Map<String ,Object> deleteMap=new HashMap<>();
            deleteMap.put("user_id",userId);
            baseMapper.deleteByMap(deleteMap);
            List<DaUserFlhEntity> flhEntityList=new ArrayList<>();
            Snowflake snowflake= IdUtil.createSnowflake(1, 1);
            flhs.forEach(flh->{
                DaUserFlhEntity daUserFlhEntity=new DaUserFlhEntity();
                daUserFlhEntity.setFlh(flh);
                daUserFlhEntity.setUserId(userId);
                daUserFlhEntity.setRecid(snowflake.nextIdStr());
                flhEntityList.add(daUserFlhEntity);
            });
            if(CollectionUtil.isNotEmpty(flhEntityList)){
                this.saveBatch(flhEntityList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}
